<?php
namespace Home\Controller;
use Think\Controller;

/**
* 评价控制器
*/
class CommentController extends Controller{
	public function add(){
		$data = I('post.');
		if(!$data['user_id'] || !$data['activity_id']){
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}
		$my_id = get_user();
		$activi = M('activity')->where('id='.$data['activity_id'])->find();

		$res  = M('comment')->where(array('user_id'=>$my_id,'to_user_id'=>$data['user_id'],'activity_id'=>$data['activity_id']))->find();
		if($res){$this->ajaxReturn(array('status'=>0,'message'=>'重复评价'));}
		//将是否评论的字段is_remark 设置为1
		//若是自己发布的活动
		if($my_id == $activi['user_id']){
			$acLog = M('activity_log')->where(array('activity_id'=>$data['activity_id'],'is_invited'=>2))->find();
			if($acLog['is_gift'] == 0){
				$this->ajaxReturn(array('status'=>0,'message'=>'参与人还没未领取见面礼红包，不能评价'));
			}
			M('activity')->where('id='.$data['activity_id'])->save(array('is_remark'=>1));
		}else{
			$acLog = M('activity_log')->where(array('activity_id'=>$data['activity_id'],'is_invited'=>2))->find();
			if($acLog['is_gift'] == 0){
				$this->ajaxReturn(array('status'=>0,'message'=>'您还未领取见面礼红包，不能评价'));
			}
			//如果是自己参与的活动
			M('activity_log')->where(array('user_id'=>$my_id,'activity_id'=>$data['activity_id']))
							->save(array('is_remark'=>1));
		}

		$like = $data['like'] ? implode(",",$data['like']) : '';
		$not_like =$data['not_like'] ? implode(",",$data['not_like']) : '';
		$this->addImpress($data);
		$addData = array(
			'user_id' => get_user(),
			'to_user_id' => $data['user_id'],
			'activity_id'=> $data['activity_id'],
			'like'		=> $like,
			'not_like'  => $not_like
		);
		M('comment')->add($addData);
		$this->ajaxReturn(array('status'=>1,'message'=>'评价成功'));
	}
	//添加印象
	public function addImpress($data){
		$like = $data['like'];
		$not_like = $data['not_like'];
		//先查找是否存在印象
		$impress = M('impress')->where('user_id='.$data['user_id'])->find();

		//如果存在印象，就是把新的评价更新到以前的评价上
		//如果不存在印象，就是把评价直接添加到数据库
		if($impress){
			if(!empty($like)){
				foreach ($like as $key => $value) {
					switch ($value) {
						case '守时':
							$impress['timeKeeping'] = $impress['timeKeeping'] +１;
							break;
						case '开朗健谈':
							$impress['open'] = $impress['open'] +１;
							break;
						case '聊得来':
							$impress['chat'] = $impress['chat'] +１;
							break;
						default:
							break;
					}
				}
			}
			if(!empty($not_like)){
				foreach ($not_like as $key => $value) {
					switch ($value) {
						case '迟到':
							$impress['late'] = $impress['late'] +１;
							break;
						case '傲娇':
							$impress['pride'] = $impress['pride'] +１;
							break;
						case '沟通不畅':
							$impress['communication'] = $impress['communication'] +１;
							break;
						default:
							$impress['fail_join'] = $impress['fail_join'] +１;
							break;
					}
				}
			}
			M('impress')->where('user_id='.$impress['user_id'])->save($impress);
		}else{
			$impress = array();
			$impress['user_id'] = $data['user_id'];
			if(!empty($like)){
				foreach ($like as $key => $value) {
					switch ($value) {
						case '守时':
							$impress['timeKeeping'] = １;
							break;
						case '开朗健谈':
							$impress['open'] = １;
							break;
						case '聊得来':
							$impress['chat'] = １;
							break;
						default:
							break;
					}
				}
			}
			if(!empty($not_like)){
				foreach ($not_like as $key => $value) {
					switch ($value) {
						case '迟到':
							$impress['late'] = １;
							break;
						case '傲娇':
							$impress['pride'] = １;
							break;
						case '沟通不畅':
							$impress['communication'] = １;
							break;
						default:
							$impress['fail_join'] = １;
							break;
					}
				}
			}
			M('impress')->add($impress);
		}
	}

	//获取评价
	public function getContent(){
		$user_id = I('post.user_id');  //要评价人的id
		if (!$user_id) {
			$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
		}

		$user = M('user')->field("username,photo_id,sex,age,star")->where('id='.$user_id)->find();
		if(!$user){$this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));}
		//查找印象评价
		if(1 == $user['sex']){
			$like_where['type'] = 1;
			$like_where['sex'] = array('neq',2);
			$like = M('comment_content')->where($like_where)->getField("content",true);
			$not_where['type'] = 2;
			$not_where['sex'] = array('neq',2);
			$not_like = M('comment_content')->where($not_where)->getField("content",true);
		}
		else{
			$like_where['type'] = 1;
			$like_where['sex'] = array('neq',1);
			$like = M('comment_content')->where($like_where)->getField("content",true);
			$not_where['type'] = 2;
			$not_where['sex'] = array('neq',1);
			$not_like = M('comment_content')->where($not_where)->getField("content",true);
		}

		$list = array(
			'user'	=> $user,
			'like'	=> $like,
			'not_like' => $not_like
		);
		$this->ajaxReturn(array('status'=>1,'message'=>'请求成功','user'=>$user,'like'=>$like,'not_like'=>$not_like));
	}
}